package wire

import (
	"github.com/google/wire"
	grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
	grpcRecovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
	regionSdk "github.com/wangzhe1991/grpc-sdk/pb/region"
	userSdk "github.com/wangzhe1991/grpc-sdk/pb/user"
	"google.golang.org/grpc"

	"gitee.com/krio/grpc-go-server/cmd/middleware"
	"gitee.com/krio/grpc-go-server/handler/region"
	"gitee.com/krio/grpc-go-server/handler/user"
	regionSvc "gitee.com/krio/grpc-go-server/internal/service/region"
	userSvc "gitee.com/krio/grpc-go-server/internal/service/user"
)

// 将New方法声明为Provider，表示New方法可以创建一个被别人依赖的对象,也就是xxx对象
var (
	// Handler
	HandlerProviderSet = wire.NewSet(
		user.NewUserHandler,
		region.NewCountryHandler,
	)
	// Service
	ServiceProviderSet = wire.NewSet(
		userSvc.NewUserService,
		regionSvc.NewCountryService,
	)
)

func newGRPCServer(
	userHdl *user.UserHandler,
	countryHdl *region.CountryHandler,
) *grpc.Server {
	s := grpc.NewServer(
		grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer( // 简单拦截器
			middleware.LogGRPCInfo(),
			grpcRecovery.UnaryServerInterceptor(),
		)),
	)
	userSdk.RegisterUserServer(s, userHdl)
	regionSdk.RegisterCountryServer(s, countryHdl)

	return s
}
